package com.innovation.chengzhangriji.ui.adapter;

import android.graphics.drawable.AnimationDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.util.ChZhRJUtils;

public class DTAudioHolder extends DTHolder {

    public interface AudioListener {

        void onClickAudio(DTAudioHolder holder);

        void downloadAudio(DTAudioHolder holder);

    }

    private static final int MIN_WIDTH = 64;

    private static final int HEIGHT = 50;

    private static final int PARENT_PADDING = 16;

    private static final int TEXT_WIDTH = 20;

    private static final int MAX_LENGTH = 60;

    private AudioListener audioListener;

    private final int grow;

    private FrameLayout layoutAudio;

    private TextView textAudio;

    private float density;

    private ImageView imageAudio;

    public DTAudioHolder(View itemView) {
        super(itemView);
        View view = LayoutInflater.from(itemView.getContext()).inflate(R.layout.item_discover_audio, getContent());
        layoutAudio = (FrameLayout) view.findViewById(R.id.layout_audio);
        layoutAudio.setOnClickListener(this);
        imageAudio = (ImageView) view.findViewById(R.id.image_audio);
        textAudio = (TextView) view.findViewById(R.id.text_audio);
        density = itemView.getResources().getDisplayMetrics().density;
        int width = itemView.getResources().getDisplayMetrics().widthPixels;
        float max = (width - MIN_WIDTH * density - PARENT_PADDING * 2 * density - TEXT_WIDTH * density);
        grow = (int) (max / MAX_LENGTH);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (audioListener == null) {
            return;
        }
        switch (v.getId()) {
            case R.id.layout_audio:
                audioListener.onClickAudio(this);
                break;
        }
    }

    public void setAudioListener(AudioListener audioListener) {
        this.audioListener = audioListener;
    }

    public void setAudioLength(int length) {
        int width = Math.min((int) (MIN_WIDTH * density) + grow * length, (int)(getContentWidth() - ChZhRJUtils.dp2px(40, itemView.getResources().getDisplayMetrics())));
        layoutAudio.setLayoutParams(new RelativeLayout.LayoutParams(width, (int) (HEIGHT * density)));
        textAudio.setText(String.format("%d''", length));
    }

    public void setAudioPlay(boolean play) {
        if (play) {
            imageAudio.setBackgroundResource(R.drawable.anim_record);
            AnimationDrawable drawable = (AnimationDrawable) imageAudio.getBackground();
            drawable.start();
        } else {
            imageAudio.setBackgroundResource(R.drawable.ic_record_3);
        }
    }

    public void downloadAudio() {
        audioListener.downloadAudio(this);
    }
}
